﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Text;

namespace Admin.Core.Common.Helpers
{
    public  class NewLDAPHelper
    {

        private string dominoServer;
        /**/
        /// <summary>
        /// domino服务器地址
        /// </summary>
        public string DominoServer
        {
            get { return dominoServer; }
            set { dominoServer = value; }
        }
        private string ldapServer;
        /**/
        /// <summary>
        /// ldap服务器地址
        /// </summary>
        public string LdapServer
        {
            get { return ldapServer; }
            set { ldapServer = value; }
        }

        private string userName;
        /**/
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        private string password;
        /**/
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        /**/
        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        public NewLDAPHelper(string userName, string password, string ldapServer)
        {
            try
            {
                //this.LdapServer = ConfigurationManager.AppSettings["LDAPServer"].ToString();

                //this.DominoServer = ConfigurationManager.AppSettings["DominoServer"].ToString();

                //this.LdapServer = ConfigurationManager.AppSettings["NewLdapServer"].ToString();


                //this.DominoServer = "testoa";
                this.LdapServer = ldapServer;
                this.UserName = userName;
                this.Password = password;
            }
            catch
            {
                throw;
            }
        }

        /**/
        /// <summary>
        /// 通过domino LDAP服务验证用户名密码是否正确
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public bool VerifyUser()
        {
            try
            {
                DirectorySearcher searcher = prepareSearcher();

                //执行以下方法时没抛出异常说明用户名密码正确
                SearchResult rs = searcher.FindOne();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /**/
        /// <summary>
        /// Prepare DirectorySearcher
        /// </summary>
        /// <returns>DirectorySearcher</returns>
        private DirectorySearcher prepareSearcher()
        {

            DirectoryEntry root = new DirectoryEntry(this.LdapServer, this.UserName, this.Password);
            DirectorySearcher searcher = new DirectorySearcher(root);
            searcher.Filter = string.Format("(SAMAccountName={0})", userName);
            searcher.PropertiesToLoad.Add("cn");

            //LDAP搜索的过滤条件,cn为用户名，uid为用户简称

            //searcher.Filter = "(&(objectClass=dominoPerson)(|(cn=" + this.UserName +

            //")(uid=" + this.UserName + ")(mail=" + this.UserName + ")))";


            //searcher.Filter = "(&(objectClass=dominoPerson)(cn=" + this.UserName +"))";
            return searcher;

        }

    }
}
